package project.Servlet;

import org.json.JSONException;
import org.json.JSONObject;
import project.Dao.UserDao;
import project.Entity.User;


import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

import static java.lang.System.out;

public class loginServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)  {
        System.out.println("执行了doGet");
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response){
        System.out.println("执行了doPost");
    }

    protected void service(HttpServletRequest request, HttpServletResponse response){
        System.out.println("执行了login");
        try {
            process(request, response);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    protected void process(HttpServletRequest request, HttpServletResponse response) throws Exception{

        String email = request.getParameter("email");
        String password = request.getParameter("password");
        String action = request.getParameter("action");
        request.setCharacterEncoding("UTF-8");
        out.println("注册邮箱:" + email + "\n指令:" + action);
        JSONObject json = new JSONObject();

        switch (action){
            case "login":
            {
                UserDao userDao = new UserDao();
                List<User> list = userDao.getUserByEmail(email);
                if(list.size()==0){
                    json.put("result", 0);
                    json.put("reason", "该邮箱未注册！");
                }
                else {
                    if (list.get(0).getEmail().equals(email) && list.get(0).getPassword().equals(password))
                    {
                        json.put("result", 1);
                    }
                    else {
                        json.put("result", 0);
                        json.put("reason", "密码错误！");
                    }
                }
                break;
            }


            case "getInfo":
            {
                UserDao userDao = new UserDao();
                List<User> list = userDao.getUserByEmail(email);
                if(list.size()==0){
                    json.put("result", 0);
                    json.put("reason", "获取信息失败！");
                }
                else {
                    json.put("result",1);
                    json.put("name",list.get(0).getName());
                    json.put("url",list.get(0).getUrl());
                    json.put("time",list.get(0).getTime());
                }
                break;
            }

            case "update_name":
            {
                UserDao userDao = new UserDao();
                List<User> list = userDao.getUserByEmail(email);
                if(list.size()==0){
                    json.put("result", 0);
                    json.put("reason", "获取信息失败！");
                }
                else {
                    String name = request.getParameter("name");
                    User user = list.get(0);
                    user.setName(name);
                    if(userDao.updateUser(user)){
                        json.put("result", 1);
                    }
                }
                break;
            }
        }


        out.println(json);
        //将响应消息返回页面
        response.setContentType("text/html; charset=UTF-8");
        response.getWriter().print(json);
        response.getWriter().flush();
        response.getWriter().close();


    }
}
